<template>
  <div class="styled-input" :class="{ filled: isFocused || modelValue }" @focusin="handleFocus" @focusout="handleBlur">
    <input
      ref="inputRef"
      type="text"
      class="styled-input__input"
      :value="modelValue"
      @input="$emit('update:modelValue', $event.target.value)"
      autocomplete="off"
      spellcheck="false"
      placeholder=""
    />
    <div class="styled-input__placeholder" aria-hidden="true">
      <span class="styled-input__placeholder-text">
        <span
          v-for="(char, idx) in chars"
          :key="idx"
          :class="['letter', { active: isFocused || modelValue }]"
          :style="{ 'animation-delay': idx * 0.05 + 's' }"
        >
          {{ char === ' ' ? '\u00A0' : char }}
        </span>
      </span>
    </div>
    <div class="styled-input__circle"></div>
  </div>
</template>

<script setup>
  import { ref, watch, computed } from 'vue';

  const props = defineProps({
    modelValue: String,
    label: { type: String, required: true },
  });

  const emit = defineEmits(['update:modelValue']);

  const isFocused = ref(false);
  const inputRef = ref(null);

  // 把 label 拆成字符数组
  const chars = computed(() => [...props.label]);

  function handleFocus() {
    isFocused.value = true;
  }

  function handleBlur() {
    if (!inputRef.value.value) {
      isFocused.value = false;
    }
  }
</script>

<style scoped>
  /* 复制并微调你原始CSS的样式及动画 */

  .styled-input {
    position: relative;
    margin-bottom: 25px;
    border: 1px solid rgba(255 255 255 / 0.2);
    border-radius: 3px;
    transition: border-color 0.3s ease;
    cursor: text;
    overflow: hidden;
  }

  .styled-input:hover {
    border-color: rgba(255 255 255 / 0.4);
  }

  .styled-input.filled {
    border-color: rgba(255 255 255 / 0.2);
  }

  .styled-input__input {
    width: 100%;
    font-size: 14px;
    padding: 18px 15px;
    background: none;
    border: none;
    outline: none;
    color: #ceafff;
    font-weight: 600;
    letter-spacing: 0.035em;
    position: relative;
    z-index: 2;
  }

  .styled-input__placeholder {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    padding-left: 45px;
    pointer-events: none;
    user-select: none;
    color: white;
    overflow: visible;
    z-index: 1;
  }

  .styled-input__placeholder-text {
    perspective: 500px;
    display: inline-block;
  }

  .letter {
    display: inline-block;
    vertical-align: middle;
    position: relative;
    animation-fill-mode: forwards;
    animation-timing-function: ease;
    animation-duration: 0.25s;
    animation-name: letterAnimOut;
    text-shadow: 0 0 5px;
    opacity: 1;
    color: white;
  }

  .letter.active {
    animation-name: letterAnimIn;
    color: #00ff6b;
  }

  @keyframes letterAnimIn {
    0% {
      transform: translate(0, 0);
      color: white;
      opacity: 1;
    }
    25% {
      transform: translate(0, 10px);
      color: red;
    }
    45% {
      opacity: 0;
      transform: translate(0, 10px);
      color: red;
    }
    55% {
      opacity: 0;
      transform: translate(0, 10px);
    }
    56% {
      transform: translate(-30px, -27px);
      opacity: 0;
      color: #00ff6b;
    }
    76% {
      transform: translate(-30px, -27px);
      opacity: 1;
      color: #00ff6b;
    }
    100% {
      transform: translate(-30px, -27px);
      opacity: 1;
      color: #00ff6b;
    }
  }

  @keyframes letterAnimOut {
    0% {
      transform: translate(-30px, -27px);
      opacity: 1;
      color: #00ff6b;
    }
    25% {
      transform: translate(-30px, -40px);
      opacity: 0;
    }
    45% {
      transform: translate(0, 10px);
      opacity: 0;
    }
    55% {
      transform: translate(0, 10px);
      opacity: 0;
      color: red;
    }
    56% {
      transform: translate(0, 10px);
      color: red;
    }
    100% {
      transform: translate(0, 0);
      opacity: 1;
      color: white;
    }
  }

  /* 涟漪效果 */

  .styled-input__circle {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    border-radius: 3px;
    overflow: hidden;
    z-index: 0;
    pointer-events: none;
    transition: all 0.3s ease;
  }

  .styled-input.filled .styled-input__circle::after {
    content: '';
    position: absolute;
    left: 16.5px;
    top: 19px;
    height: 14px;
    width: 14px;
    border-radius: 50%;
    background: rgba(255 255 255 / 0.15);
    box-shadow: 0 0 10px rgba(255 255 255 / 0);
    animation: rippleScale 0.6s ease forwards;
    opacity: 1;
  }

  @keyframes rippleScale {
    0% {
      transform: scale(1);
      opacity: 1;
    }
    100% {
      transform: scale(37);
      opacity: 0;
    }
  }
</style>
